/*
 * Copyright (C) 2009 2DAI
 *
 * Este programa es Software Libre; usted puede redistribuirlo
 * y/o modificarlo bajo los términos de la "GNU General Public
 * License" como lo publica la "FSF Free Software Foundation",
 * o (a su elección) de cualquier versión posterior.
 *
 * Este programa es distribuido con la esperanza de que le será
 * útil, pero SIN NINGUNA GARANTIA; incluso sin la garantía
 * implícita por el MERCADEO o EJERCICIO DE ALGUN PROPOSITO en
 * particular. Vea la "GNU General Public License" para más
 * detalles.
 *
 * Usted debe haber recibido una copia de la "GNU General Public
 * License" junto con este programa, si no, escriba a la "FSF
 * Free Software Foundation, Inc.", 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */
package data;

import on.Clubs;
import util.Errors;
import com.mysql.jdbc.ResultSet;
import java.sql.SQLException;

/**
 *
 * Author: Daniel Alberto Ambrosio
 */
public class DataClubs extends DataManager<Clubs> {

    public String tabla = "club";

    public DataClubs() {
        super();
        setTabla(tabla);
        requerir();
    }

    @Override
    public void requerir() {
        try {
            //conectamos con la base de datos
            sql.conectar();
            //hacemos la select de la tabla especifica
            ResultSet rs = (ResultSet) sql.ejecutarSelect("select " +
                    "id_club,number, name, dni, contact_person, address, city "+
//                    "number, name, dni, contact_person, address, city, " +
//                    "province, phone, cell_phone, fax, email, shirt_color," +
//                    "mark_ball"+
                    "from " + tabla);
            //vamos insertando todos los datos en el array de clubs
            while (rs.next()) {
                Clubs club = new Clubs();
                club.setId_Clubs(rs.getInt(1));
                club.setNumero(rs.getInt(2));
                club.setNombre(rs.getString(3));
                club.setDni(rs.getInt(4));
                club.setPersonaContacto(rs.getString(5));
                club.setDireccion(rs.getString(6));
                club.setPoblacion(rs.getString(7));
//                club.setProvincia(rs.getString(8));
//                club.setTelefono(rs.getInt(9));
//                club.setMovil(rs.getInt(10));
//                club.setFax(rs.getInt(11));
//                club.setEmail(rs.getString(12));
//                club.setColorCamisetas(rs.getString(13));
//                club.setMarcaBola(rs.getString(14));
                getList().add(club);
            }
        } catch (SQLException e) {
            Errors.creaError("ERROR: No se han podido conseguir los datos\n" +
                    e.getMessage(), e.getStackTrace());
        } finally {
            sql.desconectar();
        }
    }

    @Override
    public void guardar(Clubs o) {
        getList().set(index, o);
        sql.conectar();
        sql.ejectarUpdate("UPDATE" + tabla + " " +
                "number" + o.getNumero() + "," +
                "name" + o.getNombre() + "," +
                "dni" + o.getDni() + "," +
                "contact_person" + o.getPersonaContacto() + "," +
                "address" + o.getDireccion() + "," +
                "city" + o.getPoblacion() + "," +
                "province" + o.getProvincia() + "," +
                "phone" + o.getTelefono() + "," +
                "cell_phone" + o.getMovil() + "," +
                "fax" + o.getFax() + "," +
                "email" + o.getEmail() + "," +
                "shirt_color" + o.getColorCamisetas() + "," +
                "mark_ball" + o.getMarcaBola() +
                "WHERE id_club = " + o.getId_Clubs());
        sql.desconectar();
    }

    @Override
    public void insertar(Clubs o) {
        sql.conectar();
        getList().add(o);
        sql.ejectarUpdate("INSERT INTO" + tabla + "(number, name, dni, " +
                "contact_person, address, city, province, phone, cell_phone, " +
                "fax, email, shirt_color, mark_ball) " +
                " VALUES(" +
                "'" + o.getNumero() + "'," +
                "'" + o.getNombre() + "'," +
                "" + o.getDni() + "," +
                "'" + o.getPersonaContacto() + "'," +
                "'" + o.getDireccion() + "'," +
                "'" + o.getPoblacion() + "'," +
                "'" + o.getProvincia() + "'," +
                "" + o.getTelefono() + "," +
                "" + o.getMovil() + "," +
                "" + o.getFax() + "," +
                "'" + o.getEmail() + "'," +
                "'" + o.getColorCamisetas() + "'," +
                "'" + o.getMarcaBola() + "')");
        sql.desconectar();
    }

    @Override
    public void eliminar(Clubs o) {
        sql.conectar();
        getList().remove(index);
        sql.ejectarUpdate("DELETE FROM Locals" +
                "WHERE id_club =" + o.getId_Clubs());
        sql.ejectarUpdate("DELETE FROM" + tabla +
                "WHERE id_club =" + o.getId_Clubs());
        sql.desconectar();
    }

    @Override
    public void crear(String tabla) {
        sql.conectar();
        sql.ejectarUpdate("DROP TABLE IF EXISTS" + tabla + ";");
        sql.ejectarUpdate("CREATE TABLE IF NOT EXISTS" + tabla + " (" +
                "number int(11) NOT NULL, name varchar(20) not null, " +
                "dni int(8) not null, contact_person varchar(20), " +
                "address varchar(30) not null, city varchar(30) not null, " +
                "province varchar(30) not null, phone int(11) not null, " +
                "cell_phone int(11) default null, fax int(11) default null, " +
                "email varchar(40) default null, shirt_color varchar(15) not null, " +
                "mark_ball varchar(15) default null, PRIMARY KEY (id_club) )" +
                "ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
        sql.desconectar();
    }
}
